# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libid3tag
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.15.1b
pkgrel=2
pkgdesc="Library for id3 tagging (mingw-w64)"
arch=('any')
url="https://www.underbit.com/products/mad/"
license=("GPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-zlib")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs' 'strip' '!makeflags')
source=(#"ftp://ftp.mars.org/pub/mpeg/${_realname}-${pkgver}.tar.gz"
        https://sourceforge.net/projects/mad/files/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.gz
        0001-no-undefined.mingw.patch
        0002-update-ac-and-silent-rules.mingw.patch
        '10_utf16.patch'
        '11_unknown_encoding.patch'
        'CVE-2008-2109.patch'
        '006-libid3tag-0.15.1b-gperf-size_t.patch'
        'id3tag.pc'
        )
sha256sums=('63da4f6e7997278f8a3fef4c6a372d342f705051d1eeb6a46a86b03610e26151'
            '9231329d4f447eb9a21feb497f7d78f58d72dcd266a6dac03ff619809c135e28'
            '1283a038433fb515f2ffb2d6f8cc5433e40a6c3be3ca0510eafe9dd3a7151c94'
            '8aa2ef25a6560d5f82e8f1b06c080bf7bb507d63098915b9aa6614684f44af0f'
            'f58b782bef23fe393b992b74ef2fe4c5f7715b971faf9e048e65f8eb020b0c1a'
            '43ea3e0b324fb25802dae6410564c947ce1982243c781ef54b023f060c3b0ac4'
            'e85136f6f907c5e5f8f04ce5d125316ffbfc215f84cd06eea4196b651184fe89'
            '12c59fd85e1e327c96e33457e71d41018e6200cd0d567c0fc7556328f88a8f23')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0001-no-undefined.mingw.patch
  patch -p1 -i ${srcdir}/0002-update-ac-and-silent-rules.mingw.patch
  patch -p1 -i ${srcdir}/10_utf16.patch
  patch -p1 -i ${srcdir}/11_unknown_encoding.patch
  patch -Np0 -i ${srcdir}/CVE-2008-2109.patch
  patch -p1 -i ${srcdir}/006-libid3tag-0.15.1b-gperf-size_t.patch

  touch NEWS AUTHORS ChangeLog
  rm aclocal.m4
  rm Makefile.in
  # Force these files to be regenerated from the .gperf sources.
  rm compat.c frametype.c

  autoreconf -fi
}

build() {
  mkdir -p $srcdir/build-${MINGW_CHOST} && cd $srcdir/build-${MINGW_CHOST}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -D -m644 "${srcdir}/id3tag.pc" "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/id3tag.pc"
  sed -e "s|/usr|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/id3tag.pc"
}
